package com.hd.trans.network.bean;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import androidx.annotation.Nullable;

import com.hd.trans.db.DataBaseMgr;
import com.hd.trans.db.bean.HuDunLanguage;

import org.jetbrains.annotations.NotNull;

public class HdLangWrapper implements Parcelable {

    private final String countryName;
    private final HuDunLanguage companion;
    private final String displayName;

    /** 中文（简体） */
    public static final HdLangWrapper ZH = new HdLangWrapper(
            "中国",
            getLanguageByCode("zh"),
            "中文（简体）"
    );
    /** 中文（繁体） */
    public static final HdLangWrapper CHT = new HdLangWrapper(
            "中国",
            getLanguageByCode("cht"),
            "中文（繁体）"
    );
    /** 英语 */
    public static final HdLangWrapper EN = new HdLangWrapper(
            "美国",
            getLanguageByCode("en"),
            "英语"
    );
    /** 自动 */
    public static final HdLangWrapper AUTO = new HdLangWrapper(
            "自动",
            getLanguageByCode("auto"),
            "自动"
    );
    /** 文言文 */
    public static final HdLangWrapper WYW = new HdLangWrapper(
            "中国",
            getLanguageByCode("wyw"),
            "中文（文言文）"
    );
    /** 粤语 */
    public static final HdLangWrapper YUE = new HdLangWrapper(
            "中国",
            getLanguageByCode("yue"),
            "粤语"
    );
    /** 日语 */
    public static final HdLangWrapper JA = new HdLangWrapper(
            "日本",
            getLanguageByCode("ja"),
            "日语"
    );
    /** 韩语 */
    public static final HdLangWrapper KO = new HdLangWrapper(
            "韩国",
            getLanguageByCode("ko"),
            "韩语"
    );

    /**
     * @param countryName 国家名称
     * @param companion 语言对象
     * @param displayName 显示的名称
     */
    public HdLangWrapper(@NotNull String countryName, @NotNull HuDunLanguage companion, @NotNull String displayName) {
        this.countryName = countryName;
        this.companion = companion;
        this.displayName = displayName;
    }

    private HdLangWrapper(@Nullable String countryName, @Nullable String languageName, @Nullable String displayName, boolean safeCheck){
        this.countryName = TextUtils.isEmpty(countryName) ? ZH.countryName : countryName;
        this.companion = getLanguageByName(languageName);
        this.displayName = TextUtils.isEmpty(displayName) ? ZH.displayName : displayName;
    }

    protected HdLangWrapper(Parcel in) {
        this(in.readString(),in.readString(), in.readString(),true);
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(countryName);
        parcel.writeString(companion.getName());
        parcel.writeString(displayName);
    }

    public String getName(){
       return companion.getName();
    }

    public String getTransCode(){
        return companion.getTranslateCode();
    }

    public String getCountryName() {
        return countryName;
    }

    public HuDunLanguage getCompanion() {
        return companion;
    }

    public String getDisplayName() {
        return displayName;
    }


    public static final Creator<HdLangWrapper> CREATOR = new Creator<HdLangWrapper>() {
        @Override
        public HdLangWrapper createFromParcel(Parcel in) {
            return new HdLangWrapper(in);
        }

        @Override
        public HdLangWrapper[] newArray(int size) {
            return new HdLangWrapper[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    /**
     * 根据语言名称获取语言对象
     * @param name 语言名称
     */
    public static HuDunLanguage getLanguageByName(String name){
        String langName = fixLanguageName(name);
        HuDunLanguage lang = DataBaseMgr.getInstance().getLanguageByName(langName);
        if (lang == null){
            lang = getLanguageByCode("zh");
        }
        return lang;
    }

    /**
     * 翻译sdk升级，语言名称变更，需要做兼容
     */
    private static String fixLanguageName(String name) {
        switch (name){
            case "中文":
                return "中文（简体）";
            case "粤语":
                return "中文（粤语）";
            case "中文繁体":
                return "中文（繁体）";
            case "文言文":
                return "中文（文言文）";
            default:
                return name;
        }
    }

    /**
     * 根据语言编号获取语言对象
     * @param translateCode 语言编号
     */
    public static HuDunLanguage getLanguageByCode(String translateCode){
        return DataBaseMgr.getInstance().getLanguageByTransCode(translateCode);
    }

}
